#include "app_lvgl.h"
#include "lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "app_screen.h"
#include "lv_obj.h"
#include "main.h"

#include "lcd.h"

static void lv_tick_timercb(void *timer)
{
    lv_tick_inc(1);
}

static void lv_task_timercb(void *timer)
{
    lv_task_handler();
}

void lv_task_app(void *pvParameter)
{
    TickType_t lasttick = xTaskGetTickCount();
	while (1) 
    {
        lv_task_handler();
        vTaskDelayUntil(&lasttick, 1 / portTICK_PERIOD_MS);
        //vTaskDelay(5 / portTICK_PERIOD_MS);     
	}
	vTaskDelete(NULL);
}

void my_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
    lcd_fill_color(area->x1,area->y1,(const uint16_t *)color_p,area->x2-area->x1+1,area->y2-area->y1+1);
    lv_disp_flush_ready(disp_drv);
}

int mouse_x;
int mouse_y;
int mouse_sta;

#include "lvgl_YouBiao.h"
bool mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    (void) indev_drv;   

    data->point.x = mouse_x;
    data->point.y = mouse_y;
    data->state = mouse_sta ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;

    //if(SYSTEM_State.ft5206_sta)
    {
        lvgl_YouBiao_SheZhi_WeiZhi(mouse_x, mouse_y,mouse_sta);
    }
    
    return false;
}

static lv_disp_buf_t lvgl_disp_buf;
static lv_color_t lvgl_buf1[LV_HOR_RES_MAX * 10];    
static lv_color_t lvgl_buf2[LV_HOR_RES_MAX * 10];  

void lvgl_hal_init(void)
{
	lv_disp_buf_init(&lvgl_disp_buf, lvgl_buf1, lvgl_buf2, LV_HOR_RES_MAX * 10); 


    lv_disp_drv_t disp_drv;    
    lv_disp_drv_init(&disp_drv);   

	disp_drv.buffer=&lvgl_disp_buf;
	disp_drv.flush_cb = my_flush_cb;

    lv_disp_drv_register(&disp_drv);


    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);         
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = mouse_read;    
    lv_indev_drv_register(&indev_drv);

}


void app_lvgl_init(void)
{
    lvgl_hal_init();

    lvgl_YangShi_create();
    lvgl_ZhuoMian_create(NULL);
    //lvgl_ZhuJieMian_create(lv_scr_act());

    //lvgl_TianQi_create(lv_scr_act());
    //lvgl_LaoHuangLi_create(lv_scr_act());   


    //lvgl_KaiJi_create();
    //lv_demo_printer();
    //lv_demo_benchmark();
    //lv_demo_widgets();
    if(SYSTEM_State.ft5206_sta)
    {
        lvgl_YouBiao_create();

    }

	for(int i =0;i<100;i++)
    {
        lv_task_handler();
        vTaskDelay(5 / portTICK_PERIOD_MS);     
	}

    esp_timer_create_args_t timer_conf = 
    {
        .callback = lv_tick_timercb,
        .name     = "lv_tick_timer"
    };
    esp_timer_handle_t g_wifi_connect_timer = NULL;
    esp_timer_create(&timer_conf, &g_wifi_connect_timer);
    esp_timer_start_periodic(g_wifi_connect_timer, 1 * 1000U);

#if 0  

    esp_timer_create_args_t lv_task_timer_conf = {
        .callback = lv_task_timercb,
        .name     = "lv_task_timer"
    };

    esp_timer_handle_t lv_task_timer = NULL;
    esp_timer_create(&lv_task_timer_conf, &lv_task_timer);

    esp_timer_start_periodic(lv_task_timer, 10 * 1000U);
#else

    xTaskCreate(lv_task_app,"lv_task_app",1024*10,NULL,configMAX_PRIORITIES-2,NULL);
#endif

}
